﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Xml;
using System.Web;

namespace Comm
{
    public class BSystemKeyword
    {
        public string path = HttpContext.Current.Server.MapPath("/App_Data/SystemKeywords.xml");
        /// <summary>
        /// 获取集合
        /// </summary>
        /// <param name="strWhere"></param>
        /// <param name="sort"></param>
        /// <returns></returns>
        public List<MSystemKeyword> getList(string strWhere)
        {
            DataSet ds = new DataSet();
            ds.ReadXml(path);
            if (ds.Tables.Count < 1)
            {
                return null;
            }

            DataRow[] rows = ds.Tables[0].Select(strWhere);

            if (rows == null || rows.Length < 1)
            {
                return null;
            }

            List<MSystemKeyword> list = DataRowToList(rows);
            list.Sort(SortOrdinal);
            return list;
        }

        /// <summary>
        /// 排序
        /// </summary>
        /// <param name="a1"></param>
        /// <param name="a2"></param>
        /// <returns></returns>
        private int SortOrdinal(MSystemKeyword a1, MSystemKeyword a2)
        {
            return a1.Ordinal.CompareTo(a2.Ordinal);
        }

        /// <summary>
        /// 获取对象
        /// </summary>
        /// <param name="GID"></param>
        /// <returns></returns>
        public MSystemKeyword getModel(string GID)
        {
            List<MSystemKeyword> list = getList("GID='" + GID + "'");
            if (list == null || list.Count < 1)
            {
                return null;
            }
            return list[0];
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="model"></param>
        public void Add(MSystemKeyword model)
        {
            XmlDocument doc = XMLHelper.xmlDoc(path);

            XmlNode SystemKeywordNode = doc.CreateElement("SystemKeyword");
            XmlNode GIDNode = doc.CreateElement("GID");
            GIDNode.InnerText = model.GID;
            XmlNode LookExpressionNode = doc.CreateElement("LookExpression");
            LookExpressionNode.InnerText = model.LookExpression;
            XmlNode TitleNode = doc.CreateElement("Title");
            TitleNode.InnerText = model.Title;
            XmlNode AltNode = doc.CreateElement("Alt");
            AltNode.InnerText = model.Alt;
            XmlNode LinkNode = doc.CreateElement("Link");
            LinkNode.InnerText = model.Link;
            XmlNode OrdinalNode = doc.CreateElement("Ordinal");
            OrdinalNode.InnerText = model.Ordinal.ToString();
            XmlNode NewWindowNode = doc.CreateElement("NewWindow");
            NewWindowNode.InnerText = model.NewWindow.ToString();
            XmlNode EnabledNode = doc.CreateElement("Enabled");
            EnabledNode.InnerText = model.Enabled.ToString();

            SystemKeywordNode.AppendChild(GIDNode);
            SystemKeywordNode.AppendChild(LookExpressionNode);
            SystemKeywordNode.AppendChild(TitleNode);
            SystemKeywordNode.AppendChild(AltNode);
            SystemKeywordNode.AppendChild(LinkNode);
            SystemKeywordNode.AppendChild(OrdinalNode);
            SystemKeywordNode.AppendChild(NewWindowNode);
            SystemKeywordNode.AppendChild(EnabledNode);

            doc["SystemKeywords"].AppendChild(SystemKeywordNode);
            doc.Save(path);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="model"></param>
        public void Update(MSystemKeyword model)
        {
            Delete(model.GID);
            Add(model);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="model"></param>
        public void UpdateEnabled(bool Enabled, string GID)
        {
            XmlDocument doc = XMLHelper.xmlDoc(path);
            XmlNodeList nodelist = doc["SystemKeywords"].ChildNodes;
            foreach (XmlNode node in nodelist)
            {
                if (node["GID"].InnerText.Trim() == GID)
                {
                    node["Enabled"].InnerText = Enabled.ToString();
                    break;
                }
            }

            doc.Save(path);
        }
        
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="model"></param>
        public void UpdateNewWindow(bool NewWindow, string GID)
        {
            XmlDocument doc = XMLHelper.xmlDoc(path);
            XmlNodeList nodelist = doc["SystemKeywords"].ChildNodes;
            foreach (XmlNode node in nodelist)
            {
                if (node["GID"].InnerText.Trim() == GID)
                {
                    node["NewWindow"].InnerText = NewWindow.ToString();
                    break;
                }
            }

            doc.Save(path);
        }


        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="model"></param>
        public void Delete(string GID)
        {
            XmlDocument doc = XMLHelper.xmlDoc(path);
            XmlNode node = null;
            foreach (XmlNode nd in doc["SystemKeywords"].ChildNodes)
            {
                if (nd.Name == "SystemKeyword" && nd.ChildNodes[0].InnerText == GID)
                {
                    node = nd;
                }
            }
            if (node == null)
            {
                return;
            }
            doc["SystemKeywords"].RemoveChild(node);
            doc.Save(path);
        }

        /// <summary>
        /// 将DataSet转成List
        /// </summary>
        /// <param name="ds"></param>
        /// <returns></returns>
        private List<MSystemKeyword> DataSetToList(DataSet ds)
        {
            if (ds == null || ds.Tables.Count < 1 || ds.Tables[0].Rows.Count < 1)
            {
                return null;
            }

            List<MSystemKeyword> list = new List<MSystemKeyword>();
            foreach (DataRow row in ds.Tables[0].Rows)
            {
                MSystemKeyword model = new MSystemKeyword();
                model.GID = row["GID"].ToString().Trim();
                model.LookExpression = row["LookExpression"].ToString().Trim();
                model.Title = row["Title"].ToString().Trim();
                model.Alt = row["Alt"].ToString().Trim();
                model.Link = row["Link"].ToString().Trim();
                model.Ordinal = Convert.ToInt32(row["Ordinal"]);
                model.NewWindow = Convert.ToBoolean(row["NewWindow"]);
                model.Enabled = Convert.ToBoolean(row["Enabled"]);

                list.Add(model);
            }

            return list;
        }

        /// <summary>
        /// 将DataSet转成List
        /// </summary>
        /// <param name="ds"></param>
        /// <returns></returns>
        private List<MSystemKeyword> DataRowToList(DataRow[] rows)
        {
            if (rows == null || rows.Length < 1)
            {
                return null;
            }

            List<MSystemKeyword> list = new List<MSystemKeyword>();
            foreach (DataRow row in rows)
            {
                MSystemKeyword model = new MSystemKeyword();
                model.GID = row["GID"].ToString().Trim();
                model.LookExpression = row["LookExpression"].ToString().Trim();
                model.Title = row["Title"].ToString().Trim();
                model.Alt = row["Alt"].ToString().Trim();
                model.Link = row["Link"].ToString().Trim();
                model.Ordinal = Convert.ToInt32(row["Ordinal"]);
                model.NewWindow = Convert.ToBoolean(row["NewWindow"]);
                model.Enabled = Convert.ToBoolean(row["Enabled"]);

                list.Add(model);
            }

            return list;
        }
    }

    /// <summary>
    /// 系统关键词实体类
    /// </summary>
    public class MSystemKeyword
    {
        private string _gID;
        private string _lookExpression;
        private string _title;
        private string _alt;
        private string _link;
        private int _ordinal;
        private bool _newwindow;
        private bool _enabled;

        /// <summary>
        /// 主键
        /// </summary>
        public string GID
        {
            set { _gID = value; }
            get { return _gID; }
        }

        /// <summary>
        /// 匹配正则
        /// </summary>
        public string LookExpression
        {
            set { _lookExpression = value; }
            get { return _lookExpression; }
        }

        /// <summary>
        /// 标题
        /// </summary>
        public string Title
        {
            set { _title = value; }
            get { return _title; }
        }

        /// <summary>
        /// Alt
        /// </summary>
        public string Alt
        {
            set { _alt = value; }
            get { return _alt; }
        }

        /// <summary>
        /// 链接地址
        /// </summary>
        public string Link
        {
            set { _link = value; }
            get { return _link; }
        }

        /// <summary>
        /// 排序
        /// </summary>
        public int Ordinal
        {
            set { _ordinal = value; }
            get { return _ordinal; }
        }

        /// <summary>
        /// 新窗口打开
        /// </summary>
        public bool NewWindow
        {
            set { _newwindow = value; }
            get { return _newwindow; }
        }

        /// <summary>
        /// 启用
        /// </summary>
        public bool Enabled
        {
            set { _enabled = value; }
            get { return _enabled; }
        }
    }
}
